package controller.v2;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import model.BoardVO;
import model.CatVO;
import model.LessonVO;
import model.LogEarnVO;
import model.MemberVO;
import service.v2.CollegeService;
import service.v2.ConfigService;
import service.v2.MemberService;
import util.PackageUtil;
import util.PropertieUtil;
/**
 * 商学院
 * @author mao
 *
 */
@Controller
@RequestMapping("/v2/college")
public class CollegeController {
	@Autowired
	private CollegeService collegeService;
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private ConfigService configService;
	
	
	@RequestMapping("/schoolIndex")
	public String schoolIndex(@RequestHeader (value="OS", required=false) String os,@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth, @RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request,HttpServletResponse response,MemberVO memberVO,String isJSON)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		CatVO catVO =new CatVO();
		catVO.setParent_id(PropertieUtil.loadPropertie("college"));
		List<CatVO> catList = collegeService.getCatList(catVO);
		String school_open=configService.loadConfig("SCHOOL_OPEN");
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		System.out.println(school_open);
		if(!"1".equals(isJSON)){
			request.setAttribute("os",os);
			request.setAttribute("token",token);
			request.setAttribute("memberId",member_id);
			request.setAttribute("catList",catList);
			request.setAttribute("host_url",host);
			request.setAttribute("gettingStarted_url",host+version+"/college/gettingStarted");
			//request.setAttribute("gettingStarted_url","http://10.0.0.68:8080/v2/college/gettingStarted");
			request.setAttribute("courseDetail_url",host+version+"/college/courseDetail");
			//request.setAttribute("courseDetail_url","http://10.0.0.68:8080/v2/college/courseDetail");
			if(school_open.equals("N")){
				return "/app/college/course-no";
			}else{
				return "/app/college/schoolIndex";
			}
		}else{
			Map map = new HashMap();
			map.put("catList", catList);
			map.put("gettingStarted_url",host+version+"/college/gettingStarted");
			map.put("courseDetail_url",host+version+"/college/courseDetail");
			map.put("school_open", school_open);
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        return null;
		}
	}
	@RequestMapping("/courseItem")
	public String courseItem(@RequestHeader (value="OS", required=false) String os,@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,HttpServletRequest request,
			HttpServletResponse response,LessonVO lessonVO,String isJSON)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		int total =collegeService.getTotal(lessonVO);
		lessonVO.setPagination(lessonVO.getPage_index(), total+"");
		List<LessonVO> lessonList = collegeService.getList(lessonVO);
		//os="android";
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		String suffix="";
		if(screenwidth!=null){
			suffix="?x-oss-process=image/resize,w_"+(Integer.parseInt(screenwidth)/2);
		}else{
			suffix="?x-oss-process=image/resize,w_"+540;
		}
		if(!"1".equals(isJSON)){
			request.setAttribute("lessonList",lessonList);
			request.setAttribute("first_cat_id",lessonVO.getFirst_cat_id());
			request.setAttribute("os",os);
			request.setAttribute("suffix",suffix);
			request.setAttribute("courseDetail_url",host+version+"/college/courseDetail");
			//request.setAttribute("courseDetail_url","http://10.0.0.68:8080/v2/college/courseDetail");
			return "/app/college/courseItem";
		}else{
			Map map = new HashMap();
			LessonVO pagination = new LessonVO();
			pagination.setPage_index(lessonVO.getPage_index());
			pagination.setPage_size(lessonVO.getPage_size());
			pagination.setPage_count(lessonVO.getPage_count());
			pagination.setTotal(lessonVO.getTotal());
			
			map.put("pagination", pagination);
			map.put("lessonList", lessonList);
			map.put("courseDetail_url",host+version+"/college/courseDetail");
			
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        return null;
		}
	}
	
	@RequestMapping("/courseDetail")
	public String courseDetailsCatalog(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader(value = "SCREENWIDTH", required = false) String screenwidth,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request,HttpServletResponse response,LessonVO lessonVO,String isJSON)throws Exception{
		
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		request.setAttribute("courseDetail_url",host+version+"/college/courseDetail");
		if(os != null){
			os = os.toLowerCase();
		}
		//lessonVO.setId("20");
		String suffix="";
		if(screenwidth!=null){
			suffix="?x-oss-process=image/resize,w_"+screenwidth;
		}else{
			suffix="?x-oss-process=image/resize,w_"+1080;
		}
		LessonVO reLessonVO =collegeService.getCourse(lessonVO);
		List<CatVO> clist = new ArrayList<CatVO>();
		if(reLessonVO.getCat_id()!=null){
			clist=collegeService.getCat2List(reLessonVO);
		}
		if(!"1".equals(isJSON)){
			request.setAttribute("clist",clist);
			request.setAttribute("suffix",suffix);
			request.setAttribute("reLessonVO",reLessonVO);
			request.setAttribute("os",os);
			return "/app/college/course-detail";

		}else{
			Map map = new HashMap();
			
			map.put("clist",clist);
			map.put("reLessonVO",reLessonVO);
			
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        return null;
		}
	}
	@RequestMapping("/courseDetailsOpinion")
	public String courseDetailsOpinion(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request,HttpServletResponse response,MemberVO memberVO)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		request.setAttribute("os",os);
		return "/app/college/course-details-opinion";
	}
	@RequestMapping("/gettingStarted")
	public String gettingStarted(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request,HttpServletResponse response,MemberVO memberVO)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		request.setAttribute("os",os);
		request.setAttribute("token",token);
		request.setAttribute("memberId",member_id);
		request.setAttribute("first_cat_id",request.getParameter("first_cat_id"));
		request.setAttribute("courseDetail_url",host+version+"/college/courseDetail");
		//request.setAttribute("courseDetail_url","http://10.0.0.68:8080/v2/college/courseDetail");
		return "/app/college/getting-started";
	}
}
